package com.polaris.live.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.manager.CoroutineScopeManager
import com.polaris.live.common.resp.UserPerkVo
import com.polaris.live.common.resp.UserResp
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.WidgetIdBinding
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.viewscope.autoDisposeScope
import com.polaris.live.widget.bubble.DownLoadFileUtil
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * ID控件
 *
 * @author Created by 天晴 on 2024/9/14 10:41
 */
class IdView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetIdBinding.inflate(LayoutInflater.from(context), this)

    private var numberId = ""

    private var showCopy = true

    private var defaultTextColor = 0

    init {
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.IdView)
        showCopy = typedArray.getBoolean(R.styleable.IdView_showCopy, true)
        defaultTextColor =
            typedArray.getColor(R.styleable.IdView_defaultTextColor, 0)
        typedArray.recycle()

        mBinding.imgCopy.visibility = if (showCopy) View.VISIBLE else View.GONE
        mBinding.ivCopy.visibility = if (showCopy) View.VISIBLE else View.GONE

        mBinding.root.setOnSingleClickListener {
            ClipboardUtils.copyText(numberId)
            ToastGravity.showGravity(StringUtils.getString(string.copy_success))
        }
    }

    /**
     * 设置Id
     * UserPerkVo 用户靓号
     * userId 用户userId
     */
    fun setId(user: UserResp) {
        val perk = user.perk
        if (perk != null && (perk.vanityNumberColor != null || perk.vanityNumber != null)) {
            setPerk(perk, user.userNumber ?: 0)
        } else {
            setDef(user.userNumber ?: 0)
        }
    }

    /**
     * 设置Id
     * UserPerkVo 用户靓号
     * userId 用户userId
     */
    fun setId(userBasicVo: UserBasicVo) {
        val perk = UserPerkVo(userBasicVo.numberMedal, userBasicVo.vanityNumber, userBasicVo.vanityNumberColor)
        if (perk.vanityNumberColor != null || perk.vanityNumber != null) {
            setPerk(perk, userBasicVo.userNumber ?: 0)
        } else {
            setDef(userBasicVo.userNumber)
        }
    }

    /**
     * 设置Id
     * UserPerkVo 用户靓号
     * userId 用户userId
     */
    fun setId(infoOtherResp: InfoOtherResp) {
        val perk = infoOtherResp.perk
        if (perk != null && (perk.vanityNumberColor != null || perk.vanityNumber != null)) {
            setPerk(perk, infoOtherResp.userNumber)
        } else {
            setDef(infoOtherResp.userNumber)
        }
    }

    private fun setPerk(perk: UserPerkVo, userNumber: Long) {
        mBinding.beautifulNumberLl.visibility = VISIBLE
        mBinding.userLl.visibility = GONE
        numberId = (perk.vanityNumber ?: userNumber).toString()
        val numberMedal = perk.numberMedal
        if (perk.vanityNumber != null && numberMedal != null) {
            mBinding.idFl.visibility = View.VISIBLE
            if (numberMedal.endsWith(PagConst.PAG)) {
                mBinding.pagId.visibility = VISIBLE
                mBinding.imgId.visibility = GONE
                autoDisposeScope.launch {
                    DownLoadFileUtil.getPagPath(numberMedal)?.let { netPath ->
                        withContext(CoroutineScopeManager.mainDispatcher) {
                            mBinding.pagId.apply {
                                path = netPath
                                setRepeatCount(-1)
                                play()
                            }
                        }
                    }
                }
            } else {
                mBinding.pagId.visibility = GONE
                mBinding.imgId.visibility = VISIBLE
                Glide.with(context).load(ImageUtils.imgPath(numberMedal))
                    .into(mBinding.imgId)
            }
        } else {
            mBinding.idFl.visibility = View.GONE
            numberId = StringUtils.getString(string.id, numberId)
        }
        mBinding.idText.text = numberId

        val colorId = if (defaultTextColor == 0) ColorUtils.getColor(R.color.color_2e2e2e) else defaultTextColor
        val colorString: String? = perk.vanityNumberColor?.let {
            if (it.startsWith("#") && it.length == 7) {
                it
            } else {
                null
            }
        }

        val color = try {
            colorString?.let {
                Color.parseColor(it)
            } ?: colorId
        } catch (e: IllegalArgumentException) {
            colorId
        }
        mBinding.idText.setTextColor(color)
        mBinding.imgCopy.setColorFilter(color)

    }

    private fun setDef(userNumber: Long) {
        mBinding.beautifulNumberLl.visibility = GONE
        mBinding.userLl.visibility = VISIBLE
        numberId = userNumber.toString()
        mBinding.tvUid.text = StringUtils.getString(string.id, "$userNumber")

        val colorId = if (defaultTextColor == 0) ColorUtils.getColor(R.color.color_2e2e2e) else defaultTextColor
        mBinding.tvUid.setTextColor(colorId)
        mBinding.ivCopy.setColorFilter(colorId)

        if (defaultTextColor == 0) {
            mBinding.ivCopy.setImageResource(R.drawable.icon_copy)
        } else {
            mBinding.ivCopy.setImageResource(R.drawable.icon_copy_gray)
        }
    }

}